// pages/shopSubmit/shopSubmit.js
import Toast from '../../dist/toast/toast';
const util = require('../../utils/util.js')
const app = getApp();
const tempUrl = app.globalData.cloudUrl+"shopItem1.png";
const tempUrl2 = app.globalData.cloudUrl+"shopItem2.png";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    app:app,
    cloudUrl:app.globalData.cloudUrl,
    submitList:[{name:"兰雪茶叶礼盒",img:tempUrl2,price:398,oldPrice:428},{name:"农家天然笋干",img:tempUrl,price:58,oldPrice:68}],
    selectType:0,//0商家配送，1门店自提
    selectCss:[{color:"rgb(116, 143, 231)",border:"4rpx solid rgb(116, 143, 231)"},
                {color:"grey",border:""}],
    selectDesc:"创建收获地址",
    totalPrice:0,
    totalNum:0,
    showAddress:false,
    address:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: "确认订单",
    })

    var arr = JSON.parse(options.arr);
    var total = 0;
    var num = 0;
    const list = arr;
    for(var i=0; i<list.length; i++){
      if(list[i].select){
        num += list[i].buyNum;
        total += list[i].buyNum * list[i].price;
      }
    }
    this.setData({totalNum:num,totalPrice:total,submitList:arr});
    this.getUserInfo();
  },
  getUserInfo(){
    util.getAndSetUserInfo(this.onUserBack);
  },
  onUserBack(code){
    console.log("get userinfo result:"+code);
    const app = getApp();
    const list = app.globalData.userInfo.addressList.concat();
    var flag = this.data.showAddress;
    var ads = null;
    for(var i=0; i<list.length; i++){
      if(list[i].select){
        flag = true;
        ads = list[i];
        break;
      }
    }
    this.setData({showAddress:flag,address:ads});
  },
  onAddressClick(e){
    if(this.data.selectType == "0"){
      util.gotoShopAddressPage();
    }
    else{
      util.gotoShopConsigneePage();
    }
  },
  onTypeClick(e){
    console.log(e);
    const idx = e.currentTarget.dataset.index;
    if(idx >= 1){
      Toast("功能暂未开放");
      return;
    }
    const css = this.data.selectCss;
    var desc;
    for(var i=0; i<css.length; i++){
      if(i == idx){
        css[i].color = "rgb(116, 143, 231)";
        css[i].border = "4rpx solid rgb(116, 143, 231)";
      }
      else{
        css[i].color = "grey";
        css[i].border = "";
      }
    }
    if(idx == 0){
      desc = "创建收获地址";
    }
    else{
      desc = "添加取货人";
    }
    this.setData({selectType:idx,selectCss:css,selectDesc:desc});
  },
  onPayClick(e){
    if(!this.data.showAddress){
      Toast("请填写地址信息");
      return;
    }
    Toast("已提交订单，请前往我的->我的订单 页面查看");
      const app = getApp();
      const shopList = this.data.submitList;
      const curdata = {
        type:"setOrderShops",
        openid:app.globalData.openid,
        shopList:shopList,
        status:"待付款",
        address:this.data.address,
        totalNum:this.data.totalNum,
        totalPrice:this.data.totalPrice
      }
      console.log("提交商品订单：",curdata);//
      wx.cloud.callFunction({
        name: 'quickstartFunctions',
        data:curdata,
        complete: res => {
          console.log('setOrderShops: ', res);
        }
      })
    /**wx.request({
      url: 'http://1c80318f.r26.cpolar.top/', //仅为示例，并非真实的接口地址
      data: {
        x: '',
        y: ''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
      }
    })**/
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})